package com.glory.cqaospda.viewmodel

import android.Manifest
import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import com.glory.cqaospda.model.TaskRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.CallObserveBean
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils
import pub.devrel.easypermissions.EasyPermissions
import java.util.HashMap

class HomeModel : BaseViewModel() {

    private val repository by lazy { TaskRepository() }

    fun initUser(context: AppCompatActivity){
        requestPermission(context)
        detectionUpdate()
    }

    private fun detectionUpdate() {
        val map: Map<String, String> = HashMap()
        launchOnlyResult({
            val defaultUpdate = repository.defaultUpdate(
                map)
            defUI.callObserve.value = CallObserveBean(2, defaultUpdate)
        })
    }

    private fun requestPermission(context: Activity){
        //Manifest.permission.READ_PHONE_STATE 申请不到
        val perms = arrayOf(
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
        //权限判断，第一次弹出系统的授权提示框
        if (EasyPermissions.hasPermissions(context, *perms)) {
            //@AfterPermissionGranted 有权限直接执行 todo...
            //login();
        } else {
            //没有权限的话，先提示，点确定后弹出系统的授权提示框
            EasyPermissions.requestPermissions(context, "程序运行需要权限",
                10, *perms)
        }
    }
}